import logging

from django.http import Http404
from django.shortcuts import render
from django.views import View

from course import models

logger = logging.getLogger("django")


def course_list(request):
    courses = models.Course.objects.only("title","cover_url","teacher__name",
                                         "teacher__positional_title").filter(is_delete=False)
    return render(request, "course/course.html", locals())


class CourseDetailView(View):
    """
    视频播放
    /course/<int:course_id>/
    """
    def get(self,request,course_id):
        try:
            course = models.Course.objects.only("title","cover_url","video_url","profile",
                                                "outline","teacher__name","teacher__avatar_url",
                                                "teacher__positional_title","teacher__profile").\
                select_related("teacher").filter(is_delete=False,id=course_id).first()
            return render(request,"course/course_detail.html",locals())
        except models.Course.DoesNotExist as  e:
            logger.info("当前课堂出现如下异常,\n{}".format(e))
            raise Http404("获取页面失败")